home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.10 Oct 93 / Fixed-Point Math / benchmark.cp next >
Encoding:
Text File  |  1993-07-23  |  2.3 KB  |  82 lines  |  [TEXT/KAHL]

  1. /***************************************
  2. * benchmark.cp
  3. * benchmark template function and main routine
  4. ***************************************/
  5.  
  6. #include "fix.h"
  7. #include "matrix.h"
  8. #include <math.h>
  9. #include <iostream.h>
  10. #include <console.h>
  11. #include <profile.h>
  12.  
  13.     // Change the 0 below to a 1 to use the profiler.
  14.     // Also turn on the following C++ compiler options:
  15.     //    "Generate profiler calls"
  16.     //    "Always generate stack frames"
  17.     //    "Use function call for inlines"
  18. const int use_profiler = 0;
  19.  
  20. const double pi = 3.14159;
  21. const int num_iterations = 1000;
  22.  
  23. template <class Number>
  24. unsigned long benchmark(matrix<Number> m,const int use_prof=0)
  25. {
  26.     unsigned long time1, time2, total;
  27.  
  28.         // Make the matrix an identity matrix, and print.
  29.     m(0,0) = m(1,1) = m(2,2) = m(3,3) = 1;
  30.     cout << "identity matrix:" << endl << m;
  31.     
  32.         // Create a rotation matrix, and print.
  33.     matrix<Number> rotation;
  34.     rotation(0,0) = rotation(1,1) = cos(pi/90);  // 2 degree rotation
  35.     rotation(0,1) = rotation(1,0) = sin(pi/90);
  36.     rotation(0,1) *= -1;
  37.     rotation(2,2) = rotation(3,3) = 1;
  38.     cout << "rotation matrix:" << endl << rotation;
  39.     
  40.         // Premultiply the matrix by the rotation matrix 1000 times.
  41.     GetDateTime(&time1);
  42.     if (use_prof) InitProfile(200,200);
  43.     for (int i=0; i<num_iterations; i++)
  44.         m = rotation * m;
  45.     if (use_prof) DumpProfile();
  46.     GetDateTime(&time2);
  47.  
  48.         // Print results.
  49.     cout << "result of " << num_iterations << " multiplications:" << endl << m;
  50.     total = time2-time1;
  51.     cout << "duration: " << total << " seconds" << endl;
  52.     return total;
  53. }
  54.  
  55. void main(int argc, char *argv[])
  56. {
  57.     unsigned long double_time, fix_time;
  58.     double   ratio;
  59.  
  60.         // The ccommand() function allows results to be easily saved to a file.
  61.     argc = ccommand(&argv);
  62.  
  63.     cout << "calling the benchmark function on a double matrix..." << endl;
  64.     matrix<double> double_matrix;
  65.     double_time = benchmark(double_matrix);
  66.     
  67.     cout << "calling the benchmark function on a fix matrix..." << endl;
  68.     matrix<fix> fix_matrix;
  69.     fix_time = benchmark(fix_matrix,use_profiler);
  70.     
  71.     if (!use_profiler)
  72.         if (fix_time > 0)
  73.             {
  74.                 ratio = (double)double_time/(double)fix_time;    
  75.                 cout << "fixed-point calculations were about " << ratio 
  76.                      << " times faster!" << endl;
  77.             }
  78.         else
  79.             cout << "fixed-point calculations completed in less than 1 second!" << endl;
  80. }
  81.  
  82.